//
//  WYPersonController.m
//  Boss
//
//  Created by admin on 2018/6/1.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYPersonController.h"
#import "WYPersonHeaderView.h"
#import "WYPersonCell.h"
#import "WYWaltController.h"
#import "WYMyRedPacketController.h"
#import "WYMyFollowController.h"
#import "WYOpportunityController.h"
#import "WYReplyController.h"
#import "WYInvitationBossController.h"
#import "WYBOSSTeamController.h"
#import "WYPersonMessageController.h"
#import "WYSettingController.h"
#import "WYMessageController.h"
#import "WYPersonHomePageController.h"
#import "WYMyInfo.h"
#import "WYVIPController.h"
#import "CJNewVipController.h"
#import "UIBarButtonItem+Badge.h"
#import "WYMessageListController.h"
#import "WYContactCustomerController.h"

#import "CJAuthenticationController.h"


#import "CJMYCompanyController.h"

#import "CJAssetsController.h"

@interface WYPersonController ()<UITableViewDelegate,UITableViewDataSource,WYPersonHeaderViewDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *imageArray;
@property (nonatomic,strong)NSArray *leftNameArray;
@property (nonatomic,strong)NSArray *rightNameArray;
@property (nonatomic,strong)WYMyInfo *info;



@end

@implementation WYPersonController

-(NSArray *)imageArray
{
    if (!_imageArray) {
        _imageArray = @[@[@"tabbar_asset_selected"],@[@"Authentication_icon",@"me_page_icon",@"myCompany_icon"],@[@"BOSS_icon",@"Invitation_BOSS_icon"],@[@"service_mobile_icon",@"icon_me_list_setting"]];
    }
    
    
    return _imageArray;
}

//-(NSArray *)imageArray
//{
//    if (!_imageArray) {
//        _imageArray = @[@[@"me_page_icon",@"me_wallet_icon",@"me_redPacket_icon"],@[@"me_follow_icon",@"bussiness_collection_icon",@"me_Reply_icon"],@[@"BOSS_icon",@"Invitation_BOSS_icon",@"service_mobile_icon"]];
//
////        _imageArray = @[@[@"me_page_icon",@"me_wallet_icon",@"me_redPacket_icon"],@[@"me_follow_icon",@"bussiness_collection_icon",@"me_Reply_icon"],@[@"VIP_icon",@"BOSS_icon",@"Invitation_BOSS_icon"]];
//
//
//    }
//    return _imageArray;
//}
//-(NSArray *)leftNameArray
//{
//    if (!_leftNameArray) {
//        _leftNameArray = @[@[@"个人主页",@"我的钱包",@"我的红包"],@[@"我的关注",@"商机收藏",@"我的回复"],@[@"BOSS团",@"邀请BOSS",@"联系客服"]];
////        @[@[@"个人主页",@"我的钱包",@"我的红包"],@[@"我的关注",@"商机收藏",@"我的回复"],@[@"VIP会员",@"BOSS团",@"邀请BOSS"]];
//    }
//    return _leftNameArray;
//}

-(NSArray *)leftNameArray
{
    if (!_leftNameArray) {
        _leftNameArray = @[@[@"我的资产"],@[@"加V认证",@"我的名片",@"我的公司"],@[@"BOSS团",@"邀请BOSS"],@[@"联系我们",@"设置"]];
    }
    return _leftNameArray;
}
-(NSArray *)rightNameArray
{
    if (!_rightNameArray) {
        _rightNameArray = @[@[@""],@[@"提升个人名牌可信度",@"",@""],@[@"",@"邀请好友赢红包"],@[@"",@""]];
    }
    return _rightNameArray;
}



//-(NSArray *)rightNameArray
//{
//    if (!_rightNameArray) {
//        _rightNameArray = @[@[@"完善资料可快速提升曝光",@"",@""],@[@"",@"",@""],@[@"",@"邀请好友赢红包",@""]];
//    }
//    return _rightNameArray;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
    
}

-(void)loadMessageCount
{
    WEAKSELF
    [WYHttpServer seeAllUnreadNumberSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        
        if (success) {
            NSInteger count = [response[@"result"] integerValue];
            if (count>0) {
                if (count>99) {
                    self.navigationItem.leftBarButtonItem.badgeValue = [NSString stringWithFormat:@"%ld",99];
                    self.navigationItem.leftBarButtonItem.badgeBGColor = [UIColor redColor];
                }else{
                    self.navigationItem.leftBarButtonItem.badgeValue = [NSString stringWithFormat:@"%ld",count];
                    self.navigationItem.leftBarButtonItem.badgeBGColor = [UIColor redColor];
                }
                
            }else{
                self.navigationItem.leftBarButtonItem.badgeValue = @"0";
            }
        }
    }];
}

//-(void)loadMessageCount
//{
//    [WYHttpServer getUnreadNumberSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
//        if (success) {
//            NSInteger count = [response[@"result"] integerValue];
//            if (count>0) {
//                self.navigationItem.leftBarButtonItem.badgeValue = @" ";
//                self.navigationItem.leftBarButtonItem.badgeBGColor = [UIColor orangeColor];
//            }else{
//                self.navigationItem.leftBarButtonItem.badgeValue = @"0";
//               // self.navigationItem.leftBarButtonItem.badgeBGColor = [UIColor orangeColor];
//            }
//        }
//
//
//    }];
//}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadData];
    [self loadMessageCount];
    
    [self loadNewUserInfo];
    
}
-(void)loadNewUserInfo
{
    WEAKSELF
    [WYHttpServer getNewMyInfoSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            weakSelf.info = [WYMyInfo getMyInfoFromJson:response[@"result"]];
            [WYStoreModule storeMyInfo:weakSelf.info];
            [weakSelf.tableView reloadData];
        }
        
        
    }];
    
}



-(void)loadData
{
    WEAKSELF
    [WYHttpServer getMyInfoSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            weakSelf.info = [WYMyInfo getMyInfoFromJson:response[@"result"]];
            [weakSelf.tableView reloadData];
        }
    }];
}


-(void)setupUI
{
    [self.view addSubview:self.tableView];
   // self.tableView.contentInset = UIEdgeInsetsMake(IMAGE_HEIGHT, 0, 0, 0);
    [self.tableView registerNib:[UINib nibWithNibName:@"WYPersonCell" bundle:nil] forCellReuseIdentifier:@"WYPersonCell"];
    self.tableView.estimatedSectionHeaderHeight = 100;
    //[self wr_navBarShadowImageHidden]
    [self wr_setNavBarShadowImageHidden:YES];
   // [self.tableView addSubview:self.advView];
    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"消息_icon"] style:(UIBarButtonItemStyleDone) target:self action:@selector(messageAction:)];
//
//
//
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"setting_icon"] style:(UIBarButtonItemStyleDone) target:self action:@selector(settingAction:)];
}

-(void)messageAction:(UIBarButtonItem *)item
{
    WYMessageListController *mvc = [[WYMessageListController alloc]init];
   // WYMessageController *mvc = [[WYMessageController alloc]init];
    [self.navigationController pushViewController:mvc animated:YES];
}
-(void)settingAction:(UIBarButtonItem *)item
{
    WYSettingController *SVC = [[WYSettingController alloc]init];
    [self.navigationController pushViewController:SVC animated:YES];
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.leftNameArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    if (section==2) {
//        return 2;
//    }
    return [self.leftNameArray[section]count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYPersonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYPersonCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYPersonCell" owner:nil options:nil].firstObject;
    }
    cell.messageImageView.image = [UIImage imageNamed:self.imageArray[indexPath.section][indexPath.row]];
    cell.leftNameLabel.text = self.leftNameArray[indexPath.section][indexPath.row];
    cell.rightNameLabel.text = self.rightNameArray[indexPath.section][indexPath.row];
    if (self.info) {
        if (indexPath.row==0) {
            if (self.info.perfectStatus.integerValue) {
                cell.rightNameLabel.text = @"";
            }
        }
    }
    if (indexPath.section==2&&indexPath.row==0) {
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    self.navigationItem.leftBarButtonItem.badgeValue = @"0";
//    return;
    
    
    if (indexPath.row==0&&indexPath.section==0) {
        CJAssetsController *phc =[[CJAssetsController alloc]init];
        phc.isme = YES;
        [self.navigationController pushViewController:phc animated:YES];
    }
    
    
//    if (indexPath.row==1&&indexPath.section==1) {
//
//    }
//    if (indexPath.row==2&&indexPath.section==0) {
//        WYMyRedPacketController *mvc = [[WYMyRedPacketController alloc]init];
//        [self.navigationController pushViewController:mvc animated:YES];
//    }
    
    if (indexPath.section==1) {
        if (indexPath.row==0) {
//            WYMyFollowController *mvc = [[WYMyFollowController alloc]init];
//            [self.navigationController pushViewController:mvc animated:YES];
            CJAuthenticationController *avc =[[CJAuthenticationController alloc]init];
            [self.navigationController pushViewController:avc animated:YES];
            
            
        }
        if (indexPath.row==2) {
//            WYOpportunityController *ovc = [[WYOpportunityController alloc]init];
//            [self.navigationController pushViewController:ovc animated:YES];
            
            if (self.info.firmCertificationStatus.integerValue) {
                CJMYCompanyController *mcc = [[CJMYCompanyController alloc]init];
                [self.navigationController pushViewController:mcc animated:YES];
            }else{
                CJAuthenticationController *avc =[[CJAuthenticationController alloc]init];
                [self.navigationController pushViewController:avc animated:YES];
            }
            
            
            
            
            
            
            
            
            
        }
//        if (indexPath.row==2) {
//            WYReplyController *rvc = [[WYReplyController alloc]init];
//            [self.navigationController pushViewController:rvc animated:YES];
//        }
        
        
        
    }
    if (indexPath.section==2) {
        if (indexPath.row==1) {
            WYInvitationBossController *ivc = [[WYInvitationBossController alloc]init];
            [self.navigationController pushViewController:ivc animated:YES];
        }
        if (indexPath.row==0) {
            WYBOSSTeamController *bvc = [[WYBOSSTeamController alloc]init];
            [self.navigationController pushViewController:bvc animated:YES];
        }
//        if (indexPath.row==2) {
////            NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@", @"400-666-8055"];
////            if (@available(iOS 10.0, *)) {
////                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
////            } else {
////                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
////            }
//
//            
//
//        }
//        if (indexPath.row==0) {
//            [self gotoVipAction];
//        }
        
        
    }
    
    if (indexPath.section==3) {
        if (indexPath.row==0) {
            WYContactCustomerController *ccc= [[WYContactCustomerController alloc]init];
            [self.navigationController pushViewController:ccc animated:YES];
        }
        if (indexPath.row==1) {
            WYSettingController *svc =[[WYSettingController alloc]init];
            [self.navigationController pushViewController:svc animated:YES];
        }
    }
    
    
    
    
}



-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        WYPersonHeaderView *view = [[WYPersonHeaderView alloc]init];
        view.delegate = self;
        
        WYMyInfo *info = [WYStoreModule currentMyInfo];
       
        if (info) {
            [view setInfo:info];
        }
        [view.doneBtn addTarget:self action:@selector(clickCompleteAction) forControlEvents:(UIControlEventTouchUpInside)];
        
        
        return view;
    }
    
    return nil;
}
-(void)clickCompleteAction
{
    WYPersonMessageController *pmc = [[WYPersonMessageController alloc]init];
    [self.navigationController pushViewController:pmc animated:YES];
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        WYMyInfo *info = [WYStoreModule currentMyInfo];
        if (info) {
            if (info.userPerfectStatus.integerValue) {
                return 100;
            }else{
                return 140;
            }
        }else{
            return 100;
        }
        
        
        
        
    }
    return 0.0001;
}



-(void)gotoPersonMessage
{
    WYPersonMessageController *pmc = [[WYPersonMessageController alloc]init];
    [self.navigationController pushViewController:pmc animated:YES];
    
}
-(void)gotoVipAction
{
    
    WYPersonHomePageController *phc =[[WYPersonHomePageController alloc]init];
    [self.navigationController pushViewController:phc animated:YES];
    
    
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"IOS暂不支持开通会员服务" message:@"" preferredStyle:(UIAlertControllerStyleAlert)];
//    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        
//        [alert dismissViewControllerAnimated:YES completion:nil];
//        
//    }];
//    
//    [alert addAction:action];
//    
//    [self presentViewController:alert animated:YES completion:nil];
    
//    WYVIPController *vvc =[[WYVIPController alloc]init];
//    [self.navigationController pushViewController:vvc animated:YES];
}



//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    if (section==0) {
//       return 100;
//    }
//    return 0.001;
//}





- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-44-self.navHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    return _tableView;
}



@end
